﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraBars.Ribbon;
using DevExpress.Utils;
using DevExpress.XtraBars;

namespace Sunboxsoft.WinForm.Shell.Utils
{
    static class ApplicationMenuUtil
    {
        public static void ApplyToApplicationMenu(ApplicationMenu applicationMenu, List<ActionNode> rootMenus)
        {
            ImageCollection imageCollection = applicationMenu.Ribbon.Images as ImageCollection;
            ImageCollection largeImageCollection = applicationMenu.Ribbon.LargeImages as ImageCollection;
            for (int index = 0; index < rootMenus.Count; index++)
            {
                ActionNode node = rootMenus[index];

                if (node.Children != null && node.Children.Count > 0)
                {
                    if (node.UIObject != null)
                    {
                        applicationMenu.ItemLinks.Add(node.UIObject as BarItem);
                    }
                    else
                    {
                        BarSubItem barItem = new BarSubItem();
                        node.UIObject = barItem;
                        barItem.Caption = node.Caption;
                        barItem.Enabled = node.IsEnable;
                        if (node.Image != null)
                        {
                            imageCollection.Images.Add(node.Image);
                            barItem.ImageIndex = imageCollection.Images.Count - 1;
                        }

                        if (node.LargeImage != null)
                        {
                            largeImageCollection.Images.Add(node.LargeImage);
                            barItem.LargeImageIndex = largeImageCollection.Images.Count - 1;
                        }
                        applicationMenu.Ribbon.Items.Add(barItem);
                        applicationMenu.ItemLinks.Add(barItem);

                        ApplyToApplicationMenu(applicationMenu, barItem, node.Children);
                    }
                }
                else
                {
                    if (node.UIObject != null)
                    {
                        applicationMenu.ItemLinks.Add(node.UIObject as BarItem);
                    }
                    else
                    {
                        DevExpress.XtraBars.BarItem barItem = new DevExpress.XtraBars.BarButtonItem();
                        node.UIObject = barItem;
                        barItem.Caption = node.Caption;
                        barItem.Enabled = node.IsEnable;
                        if (node.ClickCallback != null)
                        {
                            barItem.ItemClick += new ItemClickEventHandler(new Action<object, object>((o1, o2) =>
                            {
                                node.ClickCallback(o1, o2);
                            }));
                        }
                        if (node.Image != null)
                        {
                            imageCollection.Images.Add(node.Image);
                            barItem.ImageIndex = imageCollection.Images.Count - 1;
                        }

                        if (node.LargeImage != null)
                        {
                            largeImageCollection.Images.Add(node.LargeImage);
                            barItem.LargeImageIndex = largeImageCollection.Images.Count - 1;
                        }
                        applicationMenu.Ribbon.Items.Add(barItem);
                        applicationMenu.ItemLinks.Add(barItem);
                    }
                }
            }
        }

        public static void ApplyToApplicationMenu(ApplicationMenu applicationMenu, BarSubItem parentItem, List<ActionNode> children)
        {
            ImageCollection imageCollection = applicationMenu.Ribbon.Images as ImageCollection;
            ImageCollection largeImageCollection = applicationMenu.Ribbon.LargeImages as ImageCollection;
            for (int index = 0; index < children.Count; index++)
            {
                ActionNode node = children[index];

                if (node.Children != null && node.Children.Count > 0)
                {
                    if (node.UIObject != null)
                    {
                        parentItem.ItemLinks.Add(node.UIObject as BarItem);
                    }
                    else
                    {
                        BarSubItem barItem = new BarSubItem();
                        node.UIObject = barItem;
                        barItem.Caption = node.Caption;
                        barItem.Enabled = node.IsEnable;

                        if (node.Image != null)
                        {
                            imageCollection.Images.Add(node.Image);
                            barItem.ImageIndex = imageCollection.Images.Count - 1;
                        }

                        if (node.LargeImage != null)
                        {
                            largeImageCollection.Images.Add(node.LargeImage);
                            barItem.LargeImageIndex = largeImageCollection.Images.Count - 1;
                        }
                        applicationMenu.Ribbon.Items.Add(barItem);
                        parentItem.ItemLinks.Add(barItem);

                        ApplyToApplicationMenu(applicationMenu, barItem, node.Children);
                    }
                }
                else
                {
                    if (node.UIObject != null)
                    {
                        parentItem.ItemLinks.Add(node.UIObject as BarItem);
                    }
                    else
                    {
                        DevExpress.XtraBars.BarItem barItem = new DevExpress.XtraBars.BarButtonItem();
                        node.UIObject = barItem;
                        barItem.Caption = node.Caption;
                        barItem.Enabled = node.IsEnable;

                        if (node.ClickCallback != null)
                        {
                            barItem.ItemClick += new ItemClickEventHandler(new Action<object, object>((o1, o2) =>
                            {
                                node.ClickCallback(o1, o2);
                            }));
                        }

                        if (node.Image != null)
                        {
                            imageCollection.Images.Add(node.Image);
                            barItem.ImageIndex = imageCollection.Images.Count - 1;
                        }

                        if (node.LargeImage != null)
                        {
                            largeImageCollection.Images.Add(node.LargeImage);
                            barItem.LargeImageIndex = largeImageCollection.Images.Count - 1;
                        }
                        applicationMenu.Ribbon.Items.Add(barItem);
                        parentItem.ItemLinks.Add(barItem);
                    }
                }
            }
        }
    }
}
